package com.canaan.business.controller.pc.api.erp;

import cn.hutool.json.JSONUtil;
import com.canaan.common.encrypt.AesCryptorUtils;
import com.canaan.common.encrypt.ApiResult;
import com.canaan.common.encrypt.ResultDTO;
import com.canaan.business.domain.basic.Owner;
import com.canaan.business.domain.vo.ErpOwnerVo;
import com.canaan.business.service.basic.IOwnerService;
import com.canaan.common.annotation.ApiInterfaceLog;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 物料主数据Controller
 *
 * @author ruoyi
 * @date 2022-11-30
 */
@Slf4j
@Api(tags = "货主")
@RestController
@RequestMapping("")
public class ErpOwnerController extends BaseController {
    @Autowired
    private IOwnerService ownerService;
    @Autowired
    private HttpCommonReq commonReq;

    /**
     * 新增物料主数据
     */
    @ApiOperation(value = "同步地址信息")
    @ApiInterfaceLog(title = "同步地址信息")
    @PostMapping("/edi/owner/sync")
    public String sync(HttpServletRequest request) {

        try {
            String json = commonReq.getCommonReq(request).getData();
            ErpOwnerVo erpOwnerVo = JSONUtil.toBean(json, ErpOwnerVo.class);
            Owner query = new Owner();
            query.setOwner(erpOwnerVo.getOwner());
            List<Owner> owners = ownerService.selectOwnerList(query);
            Owner tmp = generateOwner(erpOwnerVo);
            if (erpOwnerVo.getAction().equals("1")) {
                if (owners.size() != 0) {
                    throw new Exception(String.format("%s已经存在不可以新增", erpOwnerVo.getOwner()));
                }
                tmp.setCreateBy("ERP");
                return buildResult(true, String.valueOf(ownerService.insertOwner(tmp)));
            } else {
                if (owners.size() == 0) {
                    throw new Exception(String.format("%s不存在不可以更新", erpOwnerVo.getOwner()));
                }
                tmp.setId(owners.get(0).getId());
                tmp.setUpdateBy("ERP");
                return buildResult(true, String.valueOf(ownerService.insertOwner(tmp)));
            }
        } catch (Exception e) {
            log.error("同步货主信息异常,msg:{}", e.getMessage(), e);
            return buildResult(false, e.getMessage());
        }
    }

    /**
     * 创建货主
     *
     * @param vo
     * @return
     */
    private Owner generateOwner(ErpOwnerVo vo) {
        Owner address = new Owner();
        address.setOwner(vo.getOwner());
        address.setCity(vo.getCity());
        address.setCountry(vo.getCountry());
        address.setAddress(vo.getAddress());
        address.setName(vo.getName());
        address.setLatitudect(vo.getLatitudect());
        address.setLongitude(vo.getLongitude());
        address.setDistrict(vo.getDistrict());
        address.setProvince(vo.getProvince());
        address.setContactPersonName(vo.getContactPersonName());
        address.setContactPersonPhone(vo.getContactPersonPhone());
        address.setType(vo.getType());
        return address;
    }

    /**
     * 生成返回结果
     *
     * @param hasSuccess
     * @return
     */
    private static String buildResult(boolean hasSuccess, String defaultMsg) {
        ApiResult<ResultDTO<AjaxResult>> result = new ApiResult<>();
        ResultDTO<AjaxResult> resultDTO = new ResultDTO<>();
        AjaxResult ajaxResult = new AjaxResult();
        if (hasSuccess) {
            resultDTO.setStatus("success");
            ajaxResult.success();
        } else {
            resultDTO.setStatus("fail");
            ajaxResult.error(defaultMsg);
        }
        result.setContent(resultDTO);
        resultDTO.setData(ajaxResult);

        return AesCryptorUtils.encrypt(JSONUtil.toJsonStr(result), AesCryptorUtils.SECURITY_KEY);
    }
}
